package com.fsy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fsy.entities.ConsumeLogs;
import com.fsy.entities.MemberType;
import com.fsy.entities.ResourceRoom;
import com.fsy.service.ConsumeLogsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/consumelogs")
public class ConsumeLogsController {
    @Resource
    private ConsumeLogsService consumeLogsService;

    //使用条件构造器：分页查询
    @GetMapping("/findByConsumeLogs")
    public Map<String,Object> findByMemberType(Integer page, Integer limit, @RequestParam Map<String,String> map){
        //创建查询条件构造器
        QueryWrapper<MemberType> wrapper = new QueryWrapper<>();
        //创建查询条件构造器
        //List<MemberType> findMemberType = memberTypeService.list(wrapper);
        //wrapper.eq("deleted","0").orderByAsc("id");
        //创建分页组件,第一个参数表示当前是第几页，第二个参数表示每页几条
        Page<Map<String,Object>> pageZJ = new Page<>(page,limit);
        //分页查询结果里：recirds是数据，total是总条数，size是每页几条，current是当前第几页，pages是总共几页
        IPage<Map<String,Object>> rsl = consumeLogsService.pageMaps(pageZJ);
        Map<String,Object>  rslmap = new HashMap<>();
        rslmap.put("code","0");
        rslmap.put("msg","成功");
        rslmap.put("data",rsl.getRecords());//取数据
        rslmap.put("count",rsl.getTotal());//取总条数
        return rslmap;
    }

    //新增
    @PostMapping
    public Map<String,Object> add(@ModelAttribute ConsumeLogs consumeLogs){
        boolean save = consumeLogsService.save(consumeLogs);
        Map<String,Object> map = new HashMap<>();
        if (save){
            map.put("code","0");
            map.put("msg","新增成功");
        }else {
            map.put("code","1");
            map.put("msg","新增失败");
        }
        return map;
    }

    //修改
    @PutMapping
    public Map<String,Object> update(@ModelAttribute ConsumeLogs consumeLogs){
        boolean update = consumeLogsService.updateById(consumeLogs);
        Map<String,Object> map = new HashMap<>();
        if (update){
            map.put("code","0");
            map.put("msg","修改成功");
        }else {
            map.put("code","1");
            map.put("msg","修改失败");
        }
        return map;
    }

    //删除
    @DeleteMapping("/{id}")
    public Map<String,Object> del(@PathVariable("id") Integer id){
        boolean del = consumeLogsService.removeById(id);
        Map<String,Object> map = new HashMap<>();
        if (del){
            map.put("code","0");
            map.put("msg","删除成功");
        }else {
            map.put("code","1");
            map.put("msg","删除失败");
        }
        return map;
    }

    //删除
    @DeleteMapping("/TypeDel")
    public Map<String,Object> batchDel(List<Integer> list){
        boolean del = consumeLogsService.removeBatchByIds(list);
        Map<String,Object> map = new HashMap<>();
        if (del){
            map.put("code","0");
            map.put("msg","删除成功");
        }else {
            map.put("code","1");
            map.put("msg","删除失败");
        }
        return map;
    }

}
